package o2o.web.actions.order.thread;

import com.jinyou.utils.common.DateUtils;
import o2o.cache.order.OrderInfoMemory;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.order.ORDER_STATUS;
import o2o.dao.PO.order.ORDER_TYPE;
import o2o.dao.PO.order.OrderInfo;
import o2o.web.actions.order.OrderMan;
import o2o.web.actions.pay.OrderPay;
import o2o.system.data.SYS_SETTINGS;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//订单 检测订单待用户确认完成
public class OverOrderThread extends Thread {
    public static Map<String, OrderInfo> map = new ConcurrentHashMap<>();   //检测订单一定时间内未支付  直接取消

    private boolean threadFlag = true;

    private static OverOrderThread thread = new OverOrderThread();

    public static OverOrderThread getInstance() {
        if (thread == null) {
            thread = new OverOrderThread();
        }
        return thread;
    }

    private OverOrderThread() {
    }


    @Override
    public void run() {
        while (threadFlag) {
            try {
                Thread.sleep(1 * 60 * 1000);  //线程每次停止1分钟  zhuwx
//                Thread.sleep(3 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (map.size() > 0) {
                synchronized (map) {
                    for (Map.Entry<String, OrderInfo> data : map.entrySet()) {
                        String key = data.getKey();
                        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(key);
                        if (null == orderInfo) {
                            map.remove(key);
                            continue;
                        }

                        //开锁订单不自动完成
                        if (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType()) {
                            map.remove(key);
                            continue;
                        }

                        //检查订单状态  如果不是  店铺确认消费、配送员送达  就不需要操作
                        if (0 == ORDER_STATUS.SHOP_FINISH - orderInfo.getOrderStatus()
                                || 0 == ORDER_STATUS.POSTMAN_FINISH - orderInfo.getOrderStatus()
                                || (0 == ORDER_STATUS.PAYED - orderInfo.getOrderStatus() && null != SYS_SETTINGS.ORDER_PAID_IS_OVER && 0 == 1 - SYS_SETTINGS.ORDER_PAID_IS_OVER)) {
                        } else {
                            continue;
                        }

                        //支付成功即完成订单
                        if (0 == ORDER_STATUS.PAYED - orderInfo.getOrderStatus() && null != SYS_SETTINGS.ORDER_PAID_IS_OVER && 0 == 1 - SYS_SETTINGS.ORDER_PAID_IS_OVER) {
                            //分成
                            ResultInfo resultInfo = OrderPay.distributeMoney(orderInfo, orderInfo.getOrderNo(), "admin");// ORDER_PAID_IS_OVER 检测订单待用户确认完成
                            if (null != resultInfo && !resultInfo.isSuccess()) {
                                System.out.println("完成订单，但是分成失败：" + resultInfo.getError());
                            }

                            //完成订单
                            if (OrderMan.finishOrder(key, "admin", orderInfo)) {
                                map.remove(key);
                            }
                            continue;
                        }

                        //店铺确认消费  2小时后自动完成
                        Integer time = SYS_SETTINGS.ORDER_AUTO_FINISH_TIME;
                        if (null == time) {
                            time = 120;
                        }
                        if (((0 == ORDER_STATUS.SHOP_FINISH - orderInfo.getOrderStatus() && DateUtils.getCurrTime() - orderInfo.getShopFinishTime() > time * 60 * 1000))
                                ||  //配送员送达   2小时后自动完成
                                (0 == ORDER_STATUS.POSTMAN_FINISH - orderInfo.getOrderStatus() && DateUtils.getCurrTime() - orderInfo.getPostmanFinishTime() > time * 60 * 1000)) {

                        } else {
                            continue;
                        }

                        //完成订单
                        if (OrderMan.finishOrder(key, "admin", orderInfo)) {
                            map.remove(key);
                        }
                    }
                }
            }


        }
    }

}
